package cn.com.mrzheng.web.server.impl;

import cn.com.mrzheng.web.controller.base.BaseController;
import cn.com.mrzheng.web.entity.Auth;
import cn.com.mrzheng.web.entity.Role;
import cn.com.mrzheng.web.entity.User;
import cn.com.mrzheng.web.entity.UserRole;
import cn.com.mrzheng.web.mapper.user.IUserMapper;
import cn.com.mrzheng.web.mapper.user_role.IUserRoleMapper;
import cn.com.mrzheng.web.server.Base.BaseServiceImpl;
import cn.com.mrzheng.web.utils.AutoKey;
import cn.com.mrzheng.web.utils.Envelope;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserService extends BaseServiceImpl<User,IUserMapper> {

    @Autowired
    IUserMapper database;
    @Autowired
    IUserRoleMapper roleDatabase;
    @Autowired
    RoleService roleService;

    public Envelope login(String loginJson) {
        JSONObject jsonObject = JSONObject.parseObject(loginJson);
        Map<String,Object> map = jsonObject;
        User javaBean = database.login(map);
        Envelope<User> envelope = new Envelope<>();
        try {
            int State = 0;
            Role role = roleService.getRole(javaBean);
            List<Auth> authList = roleService.getRoleAuth(role);
            if ( !role.getId().isEmpty() && role.getId() != null ) {
                javaBean.setRole(role);
            }
            if ( !role.getId().isEmpty() && role.getId() != null ) {
                javaBean.setAuthList(authList);
            }
            if (javaBean.getId()!=null && javaBean.getId().isEmpty()) {
                State = 1;
            } else {
                State = 0;
            }
            List<User> userList = new ArrayList<>();
            userList.add(javaBean);
            envelope.setData(userList);
            BaseController.stateToEnvelope(State, envelope);
        }catch (Exception e) {
            envelope.setSuccessFlg(false);
            envelope.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return envelope;
    }

    public Integer restPassword(String restJson) {
        JSONObject jsonObject = JSONObject.parseObject(restJson);
        Map<String,Object> map = jsonObject;
        return database.restPassword(map);
    }

    public Envelope Register(String datajson) {
        Envelope<User> envelope = new Envelope<>();
        try {
            int State;
            int roleState;
            User javaBean = JSON.parseObject(datajson, User.class);
            if (javaBean.getId().equals("") || javaBean.getId() == null) {
                List<User> list = list(datajson,"1","10");
                if (list.size()<1) {
                    // 新增
                    javaBean.setId(AutoKey.getKey());
                    State = database.addEntity(javaBean);
                    // 默认 注册是用户角色
                    UserRole userRole = new UserRole();
                    userRole.setUserId(javaBean.getId());
                    userRole.setRoleId("003");
                    userRole.setId(AutoKey.getKey());
                    roleState = roleDatabase.addEntity(userRole);
                } else {
                    State = 0;
                    roleState = 0;
                }
                BaseController.stateToEnvelope(State, envelope);
                BaseController.stateToEnvelope(roleState, envelope);
            } else {
                // 编辑
                State = database.updateEntity(javaBean);
                BaseController.stateToEnvelope(State, envelope);
            }
        }catch (Exception e) {
            envelope.setSuccessFlg(false);
            envelope.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return envelope;
    }
}
